package com.drinks.yiniang.ui.activity.camera;

import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.common.camera.CameraInterface;
import com.drinks.yiniang.common.camera.album.Result;
import com.drinks.yiniang.entity.photo.Photos;
import com.drinks.yiniang.ui.widget.view.CameraSurfacePreview;
import com.drinks.yiniang.ui.widget.view.FoucsView;
import com.drinks.yiniang.utils.FileUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.zxinglibrary.scanner.camera.CameraSurface;

/**
 * 拍照
 *
 * @author han
 * @date 2019/09/16
 */
public class CameraActivity extends BaseTranBarActivity implements OnNavgationBarClickListener {

    private final int REQ_ALBUM_CODE = 0x1;//相册
    private final int REQ_TAKE_PHOTO_PREVIEW_CODE = 0x02;//拍照成功进入预览

    //private static final int TYPE_FLASH_AUTO = 0x021;
    private static final int TYPE_FLASH_ON = 0x022;
    private static final int TYPE_FLASH_OFF = 0x023;
    private int typeFlash = TYPE_FLASH_OFF;

    //nav
    private NavigationBarLayout nav_bar_camera;
    //surface view
    private CameraSurfacePreview surface_camera_view;
    //焦点view
    private FoucsView mFoucsView;
    private View v_camera_bottom;
    //相册
    private TextView tv_camera_choose_album;
    //拍照
    private TextView tv_camera_take_photo;

    @Override
    protected void initView() {
        nav_bar_camera = findViewById(R.id.nav_bar_camera);

        surface_camera_view = findViewById(R.id.surface_camera_view);

        mFoucsView = findViewById(R.id.v_camera_foucs_view);

        tv_camera_choose_album = findViewById(R.id.tv_camera_choose_album);
        tv_camera_take_photo = findViewById(R.id.tv_camera_take_photo);

        v_camera_bottom = findViewById(R.id.v_camera_bottom);

        surface_camera_view.setFoucsView(mFoucsView, v_camera_bottom);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;
    }

    @Override
    protected void initListener() {
        nav_bar_camera.setOnNavgationBarClickListener(this);
        tv_camera_choose_album.setOnClickListener(this);
        tv_camera_take_photo.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tv_camera_choose_album://相册
                Intent intent = new Intent(mContext, AlbumActivity.class);
                startActivityForResult(intent, REQ_ALBUM_CODE);
                break;
            case R.id.tv_camera_take_photo://拍照
                CameraInterface.getInstance().takePicture(new CameraInterface.TakePictureCallback() {
                    @Override
                    public void captureResult(Bitmap bitmap, boolean isVertical) {

                        Photos photos = new Photos();

                        photos.name = "p_" + System.currentTimeMillis() + ".png";
                        photos.path = FileUtils.saveBitmap(bitmap, photos.name);
                        photos.width = bitmap.getWidth();
                        photos.height = bitmap.getHeight();
                        photos.size = bitmap.getByteCount();

                        Result.addPhotos(photos);

                        if (CommonUtils.isEmptyList(Result.mPhotos)) {
                            ToastUtils.showShort("请选择图片");
                            return;
                        }

                        startActivityForResult(new Intent(mContext, PreviewPhotosActivity.class), REQ_TAKE_PHOTO_PREVIEW_CODE);
                    }
                });
                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_camera;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {
        setFlash();
    }

    @Override
    protected void onResume() {
        super.onResume();
        surface_camera_view.onResume();
        Result.clear();
    }

    @Override
    protected void onPause() {
        super.onPause();
        surface_camera_view.onPause();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constants.RESULT_CODE_NORMAL) {
            switch (requestCode) {
                case REQ_ALBUM_CODE:
                case REQ_TAKE_PHOTO_PREVIEW_CODE:
                    finish();
                    break;
                default:
                    break;
            }
        }
    }

    public void setFlash() {
        typeFlash++;
        if (typeFlash > TYPE_FLASH_OFF) {
            typeFlash = TYPE_FLASH_ON;
        }
        switch (typeFlash) {
//            case TYPE_FLASH_AUTO:
//                //mFlashLamp.setImageResource(R.drawable.ic_camera_view_flash_auto);
//                CameraInterface.getInstance().setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
//                break;
            case TYPE_FLASH_ON:
                //mFlashLamp.setImageResource(R.drawable.ic_camera_view_flash_on);
                CameraInterface.getInstance().setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                break;
            case TYPE_FLASH_OFF:
                //mFlashLamp.setImageResource(R.drawable.ic_camera_view_flash_off);
                CameraInterface.getInstance().setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        Result.clear();
        super.onDestroy();
    }
}
